package com.kyg.tjtoolmanager.controller;

import com.kyg.tjtoolmanager.App;
import com.kyg.tjtoolmanager.db.SQLiteUtils;
import com.kyg.tjtoolmanager.utils.SpeechUtil;
import javafx.fxml.FXML;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import uk.co.caprica.vlcj.factory.MediaPlayerFactory;
import uk.co.caprica.vlcj.javafx.videosurface.ImageViewVideoSurfaceFactory;
import uk.co.caprica.vlcj.media.MediaRef;
import uk.co.caprica.vlcj.media.TrackType;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventListener;
import uk.co.caprica.vlcj.player.embedded.EmbeddedMediaPlayer;

import java.net.URL;
import java.util.ResourceBundle;

public class FaceLoginDialog extends BaseController {

    @FXML
    ImageView mediaView;
    private static MediaPlayerFactory mediaPlayerFactory;
    private static EmbeddedMediaPlayer embeddedMediaPlayer;
    @FXML
    Stage stage;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //加载视频预览控件，展示人脸识别流，监听人脸核验结果
        String videoUrl = "rtsp://" + App.globalVal.faceIp + "/media/video1 RTSP/1.0";
        mediaPlayerFactory = new MediaPlayerFactory();
        embeddedMediaPlayer = mediaPlayerFactory.mediaPlayers().newEmbeddedMediaPlayer();
        mediaView.setPreserveRatio(true);
        embeddedMediaPlayer.videoSurface().set(ImageViewVideoSurfaceFactory.videoSurfaceForImageView(mediaView));
        embeddedMediaPlayer.events().addMediaPlayerEventListener(new MediaPlayerEventListener() {
            @Override
            public void mediaChanged(MediaPlayer mediaPlayer, MediaRef mediaRef) {

            }

            @Override
            public void opening(MediaPlayer mediaPlayer) {
                SQLiteUtils.insertLog(videoUrl + " opening");
            }

            @Override
            public void buffering(MediaPlayer mediaPlayer, float v) {

            }

            @Override
            public void playing(MediaPlayer mediaPlayer) {
                SQLiteUtils.insertLog(videoUrl + " playing");
            }

            @Override
            public void paused(MediaPlayer mediaPlayer) {

            }

            @Override
            public void stopped(MediaPlayer mediaPlayer) {

            }

            @Override
            public void forward(MediaPlayer mediaPlayer) {

            }

            @Override
            public void backward(MediaPlayer mediaPlayer) {

            }

            @Override
            public void finished(MediaPlayer mediaPlayer) {
                SQLiteUtils.insertLog(videoUrl + " finished");
            }

            @Override
            public void timeChanged(MediaPlayer mediaPlayer, long l) {

            }

            @Override
            public void positionChanged(MediaPlayer mediaPlayer, float v) {
//                SQLiteUtils.insertLog("positionChanged");
            }

            @Override
            public void seekableChanged(MediaPlayer mediaPlayer, int i) {

            }

            @Override
            public void pausableChanged(MediaPlayer mediaPlayer, int i) {

            }

            @Override
            public void titleChanged(MediaPlayer mediaPlayer, int i) {

            }

            @Override
            public void snapshotTaken(MediaPlayer mediaPlayer, String s) {

            }

            @Override
            public void lengthChanged(MediaPlayer mediaPlayer, long l) {

            }

            @Override
            public void videoOutput(MediaPlayer mediaPlayer, int i) {
                SQLiteUtils.insertLog(videoUrl + " videoOutput");
                //展示人脸画面
                App.StartFaceLogin = true;
            }

            @Override
            public void scrambledChanged(MediaPlayer mediaPlayer, int i) {

            }

            @Override
            public void elementaryStreamAdded(MediaPlayer mediaPlayer, TrackType trackType, int i) {

            }

            @Override
            public void elementaryStreamDeleted(MediaPlayer mediaPlayer, TrackType trackType, int i) {

            }

            @Override
            public void elementaryStreamSelected(MediaPlayer mediaPlayer, TrackType trackType, int i) {

            }

            @Override
            public void corked(MediaPlayer mediaPlayer, boolean b) {

            }

            @Override
            public void muted(MediaPlayer mediaPlayer, boolean b) {

            }

            @Override
            public void volumeChanged(MediaPlayer mediaPlayer, float v) {

            }

            @Override
            public void audioDeviceChanged(MediaPlayer mediaPlayer, String s) {

            }

            @Override
            public void chapterChanged(MediaPlayer mediaPlayer, int i) {

            }

            @Override
            public void error(MediaPlayer mediaPlayer) {
                SQLiteUtils.insertLog(videoUrl + " error");
            }

            @Override
            public void mediaPlayerReady(MediaPlayer mediaPlayer) {
                SQLiteUtils.insertLog(videoUrl + " mediaPlayerReady");
            }
        });
        embeddedMediaPlayer.media().play(videoUrl);
        SQLiteUtils.insertLog(videoUrl + " play");
    }

    @Override
    public void onShow() {
        super.onShow();
        SpeechUtil.textToSpeech("请面对人脸识别设备");
    }

    @FXML
    protected void onCancelClick() {
        closeStage();
    }

    public void closeStage() {
        closeMedia();
        stage = (Stage) rootLayout.getScene().getWindow();
        stage.close();
    }

    public static void closeMedia() {
        SQLiteUtils.insertLog("关闭人脸识别窗口");
        App.StartFaceLogin = false;
        embeddedMediaPlayer.controls().stop();
        embeddedMediaPlayer.release();
        mediaPlayerFactory.release();
    }

}
